package com.zhixinhuixue.zsyte.xxx.app

import android.app.Application
import android.content.Context
import android.content.Intent
import com.effective.android.anchors.AnchorsManager
import com.effective.android.anchors.Project
import com.tencent.bugly.crashreport.CrashReport
import com.zhixinhuixue.zsyte.xxx.BuildConfig
import me.hgj.mvvmhelper.ext.currentProcessName
import me.hgj.mvvmhelper.ext.toast
import me.hgj.mvvmhelper.util.SPUtil

/**
 * 作者　: hegaojian
 * 时间　: 2021/6/9
 * 描述　:
 */
class App : Application() {

    //全局的静态Application
    companion object {
        lateinit var instance: App
        val TAG = "BaseApplication"

    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        val processName = currentProcessName
        if (processName == packageName) {
            // 主进程初始化
            onMainProcessInit()
        } else {
            // 其他进程初始化
            processName?.let { onOtherProcessInit(it) }
        }


        CrashReport.initCrashReport(this, "e2afde091e", false);
    }

    /**
     * @description  代码的初始化请不要放在onCreate直接操作，按照下面新建异步方法
     */
    private fun onMainProcessInit() {
        AnchorsManager.getInstance()
            .debuggable(BuildConfig.DEBUG)
            //设置锚点
            .addAnchor(InitNetWork.TASK_ID, InitUtils.TASK_ID, InitComm.TASK_ID, InitToast.TASK_ID)
            .start(
                Project.Builder("app", AppTaskFactory())
                    .add(InitNetWork.TASK_ID)
                    .add(InitComm.TASK_ID)
                    .add(InitUtils.TASK_ID)
                    .add(InitToast.TASK_ID)
                    .build()
            )
    }

    /**
     * 其他进程初始化，[processName] 进程名
     */
    private fun onOtherProcessInit(processName: String) {}


    fun restartApplication() {
        "请重新登录".toast()
        SPUtil.clearByKey(instance.applicationContext, SPUtil.FILE_NAME, SPUtil.USER_INFO)
        SPUtil.clearByKey(instance.applicationContext, SPUtil.FILE_NAME, SPUtil.PASSWORD)

        val intent = instance.packageManager.getLaunchIntentForPackage(
            instance.packageName
        )
        intent!!.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
        instance.startActivity(intent)
    }


}